<template>
    <div id="employee_index">
        <!--搜索表单-->
        <div class="searchForm">
            <div class="column">
                <span>姓名：</span>
                <el-input style="height: 21px;width: 136px" placeholder="请输入姓名" v-model="searchForm.name"/>
            </div>
            <div class="column">
                <span>手机号：</span>
                <el-input style="height: 21px;width: 136px" placeholder="请输入手机号" v-model="searchForm.phone"/>
            </div>
            <div class="column">
                <span>状态：</span>
                <el-select style="width: 100px" v-model="searchForm.state"
                           @change="$forceUpdate()"
                           placeholder="请选择"
                           clearable>
                    <el-option label="正常" value="0"></el-option>
                    <el-option label="删除" value="1"></el-option>
                </el-select>
            </div>
            <div class="column">
                <el-button type="success" @click="submitSearchForm">搜索</el-button>
                <el-button type="danger" @click="newVisable=true">创建</el-button>
            </div>
        </div>
        <!--表格数据-->
        <div class="table">
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    height="450px"
                    size="medium">
                <el-table-column
                        prop="phone"
                        label="手机号">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="姓名">
                </el-table-column>
                <el-table-column
                        prop="email"
                        :show-overflow-tooltip="true"
                        label="邮箱">
                </el-table-column>
                <el-table-column
                        prop="integral"
                        label="积分">
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态">
                    <template v-slot="scope">
                        <el-tag v-if="scope.row.state=='0'" type="success">正常</el-tag>
                        <el-tag v-else type="danger">删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="info"
                        :show-overflow-tooltip="true"
                        label="备注">
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template v-slot="scope">
                        <el-button v-if="scope.row.state=='0'" size="mini" type="success" title="兑换商品"
                                   @click="exchangeProductsBtn(scope.row.id)"
                                   icon="el-icon-edit-outline"
                                   plain></el-button>
                        <el-button size="mini" type="warning" title="修改" @click="editBtn(scope.row.id)"
                                   icon="el-icon-edit"
                                   plain></el-button>
                        <el-button size="mini" type="danger" title="删除" @click="delBtn(scope.row.id)"
                                   icon="el-icon-delete"
                                   plain></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pageUtils">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="searchForm.currentPage"
                        :page-sizes="[3, 5, 7, 9]"
                        :page-size="searchForm.pageSize"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="searchForm.total">
                </el-pagination>
            </div>
        </div>

        <!--录入-->
        <el-dialog
                title="录入会员"
                :visible.sync="newVisable"
                size="mini"
                label-width="200"
                width="50%">
            <el-form :model="newForm" :rules="rules" ref="newForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="号码：" prop="phone">
                    <el-input max="11" placeholder="请输入手机号" v-model="newForm.phone"></el-input>
                </el-form-item>

                <el-form-item label="姓名：" prop="name">
                    <el-input v-model="newForm.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="邮箱：">
                    <el-input v-model="newForm.email" placeholder="如：xxx@qq.com"></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input type="textarea" v-model="newForm.info" placeholder="如：新会员"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitNewForm('newForm')">提交</el-button>
                    <el-button @click="saveCancel('newForm')">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!--修改-->
        <el-dialog
                title="修改会员"
                :visible.sync="editVisable"
                size="mini"
                label-width="200"
                width="50%">
            <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="号码：" prop="phone">
                    <el-input max="11" placeholder="请输入手机号" v-model="editForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="姓名：" prop="name">
                    <el-input v-model="editForm.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="邮箱：">
                    <el-input v-model="editForm.email" placeholder="如：xxx@qq.com"></el-input>
                </el-form-item>
                <el-form-item label="积分：">
                    <el-input v-model="editForm.integral" placeholder="如：10"></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input type="textarea" v-model="editForm.info" placeholder="如：新会员"></el-input>
                </el-form-item>
                <el-form-item label="状态：" prop="state">
                    <el-select v-model="editForm.state"
                               placeholder="请选择状态"
                               filterable
                               @change="$forceUpdate()"
                               clearable>
                        <el-option label="正常" value="0"></el-option>
                        <el-option label="删除" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitEditForm('editForm')">提交</el-button>
                    <el-button type="info" @click="resetEditForm('editForm')">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!--积分商品-->
        <el-dialog
                title="兑换积分商品"
                :visible.sync="pointProductsVisable"
                label-width="200"
                width="40%">
            <el-form :model="pointProductsForm" :rules="rules" ref="pointProductsForm" label-width="300px"
                     class="demo-ruleForm">
                <el-form-item v-if="pointProductsForm.goodsId" style="width: 80%">
                    <img width="100px" :src="pointProductsForm.coverUrl" alt="商品图片"/>
                </el-form-item>
                <el-form-item style="width: 80%" label="积分商品：" prop="goodsId">
                    <el-select @change="queryPointProductByGoodsId(pointProductsForm.goodsId)"
                               v-model="pointProductsForm.goodsId"
                               placeholder="请选择积分商品"
                               filterable
                               clearable>
                        <el-option
                                v-for="item in options_pointProducts"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="pointProductsForm.goodsId" style="width: 80%" label="所需积分：">
                    <el-input readonly type="number" v-model="pointProductsForm.integral" placeholder="所需积分"/>
                </el-form-item>
                <el-form-item style="width: 80%">
                    <el-button v-if="pointProductsForm.goodsId" type="primary"
                               @click="submitPointProductsForm('pointProductsForm')">提交
                    </el-button>
                    <el-button type="info" @click="closePointProductsForm">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

    </div>
</template>
<script>
    import {popup} from "@/assets/js/common";
    import {delMember, queryPageByQo, save, update, queryMemberById} from "@/api/member_management/member/memberApi";
    import {
        queryPointProductByGoodsId,
        queryPointProductBymemberId, saveExchangePointProductRecords
    } from "@/api/sale_management/exchange_point_products/exchangePointProductsApi";

    export default {
        data() {
            return {
                newVisable: false,
                editVisable: false,
                searchForm: {},
                tableData: [],
                newForm: {},
                editForm: {},
                rules: {
                    name: [
                        {required: true, message: '名称不能为空', trigger: 'blur'},
                    ],
                    phone: [
                        {required: true, message: '手机号不能为空', trigger: 'blur'},
                    ],
                    state: [
                        {required: true, message: '状态不能为空', trigger: 'blur'},
                    ],
                    goodsId: [
                        {required: true, message: '商品不能为空', trigger: 'blur'},
                    ]
                },
                options_pointProducts: [],
                pointProductsForm: {
                    goodsId: '',
                    coverUrl: '',
                    integral: ''
                },
                pointProductsVisable: false
            }
        },
        methods: {
            init() {
                queryPageByQo(this.searchForm).then(res => {
                    res = res.data
                    if (res.code == 200) {
                        console.log(res.data)
                        this.tableData = res.data.records
                        this.searchForm.total = res.data.total
                        this.searchForm.pageSize = res.data.size
                        this.searchForm.currentPage = res.data.current
                    } else {
                        popup(res.msg, "error")
                    }
                })
            },
            /*分页*/
            handleSizeChange(val) {
                this.searchForm.pageSize = val
                this.init()
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.searchForm.currentPage = val
                this.init()
                console.log(`当前页: ${val}`);
            },
            submitSearchForm() {
                this.init()
            },
            saveCancel(formName) {
                this.newForm = {}
                this.$refs[formName].resetFields()
                this.newVisable = false

            },
            submitNewForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        save(this.newForm).then(res => {
                            res = res.data
                            if (res.code == 200) {
                                popup("操作成功")
                                this.newForm = {}
                                this.newVisable = false
                                this.init()
                            } else {
                                popup(res.msg, "error")
                            }
                        })
                    }
                });
            },
            editBtn(id) {
                queryMemberById({id: id}).then(res => {
                    res = res.data
                    if (res.code == 200) {
                        this.editForm = {...res.data}
                        this.editVisable = true
                    } else {
                        popup(res.msg, "error")
                    }
                })
            },
            submitEditForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        update(this.editForm).then(res => {
                            res = res.data
                            if (res.code == 200) {
                                popup("操作成功")
                                this.editForm = {}
                                this.editVisable = false
                                this.init()
                            } else {
                                popup(res.msg, "error")
                            }
                        })
                    }
                })
            },
            resetEditForm(formName) {
                this.editForm = {}
                this.$refs[formName].resetFields()
                this.editVisable = false

            },
            delBtn(id) {
                this.$confirm('确定删除该会员？', '警示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delMember({id: id}).then(res => {
                        res = res.data
                        if (res.code == 200) {
                            popup("操作成功")
                            this.searchForm.state = '0'
                            this.init()
                        } else {
                            popup(res.msg, "error")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            /*兑换积分商品*/
            exchangeProductsBtn(id) {
                this.pointProductsForm = {
                    memberId:this.pointProductsForm.memberId,
                    goodsId: '',
                    coverUrl: '',
                    integral: ''
                },
                    queryPointProductBymemberId({memberId: id}).then(res => {
                        res = res.data
                        if (res.code == 200) {
                            this.options_pointProducts = res.data
                            if (!this.options_pointProducts.length) {
                                popup("该会员积分数无法兑换任何商品", "warning")
                                return;
                            }
                            this.pointProductsForm.memberId=id
                            this.pointProductsVisable = true
                        } else {
                            popup(res.msg, "error")
                        }
                    })
            },
            submitPointProductsForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        saveExchangePointProductRecords(this.pointProductsForm).then(res=>{
                            res=res.data
                            if (res.code==200){
                                popup("兑换成功")
                                this.pointProductsForm={}
                                this.pointProductsVisable=false
                                this.init()

                            }else {
                                popup(res.msg,"error")
                            }
                        })
                    }
                })

            },
            closePointProductsForm() {
                this.options_pointProducts = []
                this.pointProductsForm = {}
                this.pointProductsVisable = false

            },
            queryPointProductByGoodsId(goodsId) {
                queryPointProductByGoodsId({goodsId: goodsId}).then(res => {
                    res = res.data
                    if (res.code == 200) {
                        if (!res.data) {
                            this.pointProductsForm = {
                                memberId:this.pointProductsForm.memberId,
                                goodsId: '',
                                coverUrl: '',
                                integral: ''
                            }
                            return;
                        }
                        this.pointProductsForm.integral = res.data.integral
                        this.pointProductsForm.coverUrl = res.data.coverUrl
                    } else {
                        popup(res.msg, "error")
                        this.pointProductsForm.goodsId = ''
                    }
                })
            }
        },
        mounted() {
            /*初始化表格数据*/
            this.searchForm.state = '0'
            this.init()
        }
    }
</script>
<style>
    .searchForm {
        height: 134px;
    }

    .table {
        background-color: #303133;
        height: 320px;
    }

    .pageUtils {
        position: absolute;
        width: 84%;
        margin: 2px auto;
        height: 43px;
    }

    .searchForm {
        display: inline-block;
        margin: 0 auto;
        height: 51px;
        line-height: 51px;
    }

    .searchForm .column {
        display: inline-block;
        margin: 0 auto;
        margin-right: 2px;
        line-height: 48px;
        height: 51px;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>